package com.wj.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.taglibs.standard.tag.common.xml.IfTag;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wj.bean.Car;
import com.wj.bean.OrderList;
import com.wj.bean.Orders;
import com.wj.bean.Room;
import com.wj.bean.User;
import com.wj.service.UserService;
import com.wj.service.impl.UserServiceImpl;

@WebServlet("/UserServlet/*")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static UserService us = new UserServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String info = request.getPathInfo().substring(1);
		if (info.equals("login")) {
			login(request, response);
		} else if (info.equals("register")) {
			register(request, response);
		} else if (info.equals("listAllUser")) {
			listAllUser(request, response);
		} else if (info.equals("removeUserById")) {
			removeUserById(request,response);
		} else if (info.equals("removeAllUser")) {
			removeAllUser(request,response);
		} else if (info.equals("listAllRoom")) {
			listAllRoom(request,response);
		} else if (info.equals("updUserById")) {
			updUserById(request,response);
		} else if (info.equals("updRoomByType")) {
			updRoomByType(request,response);
		} else if (info.equals("queryRoomByType")) {
			queryRoomByType(request,response);
		} else if (info.equals("addRoomOrder")) {
			addRoomOrder(request,response);
		} else if (info.equals("listAllOrder")) {
			listAllOrder(request,response);
		} else if (info.equals("removeOrderById")) {
			removeOrderById(request,response);
		} else if (info.equals("removeAllOrder")) {
			removeAllOrder(request,response);
		} else if (info.equals("updOrder")) {
			updOrder(request,response);
		} else if (info.equals("listAllCar")) {
			listAllCar(request,response);
		} else if (info.equals("addCarOrder")) {
			addCarOrder(request,response);
		} else if (info.equals("listAllOrderCarBack")) {
			listAllOrderCarBack(request,response);
		} else if (info.equals("listAllCarBack")) {
			listAllCarBack(request,response);
		} else if (info.equals("removeCarById")) {
			removeCarById(request,response);
		} else if (info.equals("removeAllCar")) {
			removeAllCar(request,response);
		}
	}
	
	/*
	 *删除多个订单
	*/
	private void removeAllCar(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String c_ids = request.getParameter("c_ids");
		c_ids = c_ids.substring(1, c_ids.length()-1);
		String[] c_idsArr = c_ids.split(",");
		List<String> list=Arrays.asList(c_idsArr);
		int n = us.removeAllCar(list);
		response.getWriter().print(n);
	}
	/*
	 *删除单个订单
	*/
	private void removeCarById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int c_id = Integer.valueOf(request.getParameter("c_id"));
		boolean b = us.removeCarById(c_id);
		if(b) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}

	/*
	 * 查询车位列表
	*/
	private void listAllCarBack(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String search = request.getParameter("search");
		int offset = Integer.valueOf(request.getParameter("offset"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		List<Car> carList = us.listAllCarBack(search, offset, limit);
		int total = us.getCarTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", carList);
		Gson gson = new GsonBuilder().create();
		String json = gson.toJson(map);
		response.getWriter().print(json);
	}

	/*
	 * 查询预订车位列表
	*/
	private void listAllOrderCarBack(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String search = request.getParameter("search");
		int offset = Integer.valueOf(request.getParameter("offset"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		List<Car> orderList = us.listAllOrderCarBack(search, offset, limit);
		int total = us.getOrderCarTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", orderList);
		Gson gson = new GsonBuilder().create();
		String json = gson.toJson(map);
		response.getWriter().print(json);
	}

	/*
	 * 预订车位
	*/
	private void addCarOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String u_id = request.getParameter("u_id");
		List<Orders> o=us.queryOrderByUid(u_id);
		if (o.size()==0) {
			response.getWriter().print(0);
		}else {
			String c_ids = request.getParameter("c_ids");
			c_ids = c_ids.substring(1, c_ids.length()-1);
			String[] c_idsArr = c_ids.split(",");
			List<String> c_idlist=new ArrayList<>();
			for (int i = 0; i < c_idsArr.length; i++) {
				c_idsArr[i]=c_idsArr[i].substring(1, c_idsArr[i].length()-1);
				c_idlist.add(c_idsArr[i]);
			}
			int n=us.updCarOrder(c_idlist,u_id);
			response.getWriter().print(n);
		}
	}

	/*
	 * 查询车库信息
	*/
	private void listAllCar(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Car> carList = us.listAllCar();
		Map<String,Object> map = new HashMap<>();
		map.put("carList", carList);
		Gson gson = new GsonBuilder().create();
		String json = gson.toJson(map);
		response.getWriter().print(json);
	}

	/*
	 * 修改订单信息
	*/
	private void updOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int o_id = Integer.valueOf(request.getParameter("o_id"));
		String r_num = request.getParameter("r_num");
		String o_date = request.getParameter("o_date");
		int o_days = Integer.valueOf(request.getParameter("o_days"));
		int r_id=us.queryRoomByNum(r_num);
		boolean b = us.updOrder(o_id,r_id,o_date,o_days);
		if(b) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	/*
	 * 删除多个所选订单
	*/
	private void removeAllOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String o_ids = request.getParameter("o_ids");
		o_ids = o_ids.substring(1, o_ids.length()-1);
		String[] o_idsArr = o_ids.split(",");
		List<String> list=Arrays.asList(o_idsArr);
		int n = us.removeAllOrder(list);
		response.getWriter().print(n);
	}
	/*
	 *删除单个订单
	*/
	private void removeOrderById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int o_id = Integer.valueOf(request.getParameter("o_id"));
		boolean b = us.removeOrderById(o_id);
		if(b) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	/*
	 *查询订单列表 
	*/
	private void listAllOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String search = request.getParameter("search");
		int offset = Integer.valueOf(request.getParameter("offset"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		List<OrderList> orderList = us.listAllOrder(search, offset, limit);
		int total = us.getOrderTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", orderList);
		Gson gson = new GsonBuilder().create();
		String json = gson.toJson(map);
		response.getWriter().print(json);
	}
	/*
	 *添加订单 
	*/
	private void addRoomOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String r_num = request.getParameter("r_num");
		String o_date = request.getParameter("o_date");
		int u_id = Integer.valueOf(request.getParameter("u_id"));
		int o_days = Integer.valueOf(request.getParameter("o_days"));
		String price = request.getParameter("r_price");
		int x= price.indexOf('/');
		price=price.substring(0, x);
		int r_price=Integer.valueOf(price);
		int o_price=r_price*o_days;
		int r_id=us.queRoomByNumForId(r_num);
		Orders o=new Orders();
		o.setO_date(o_date);
		o.setO_days(o_days);
		o.setO_price(o_price);
		o.setR_id(r_id);
		o.setU_id(u_id);
		boolean b1=us.updRoomByNum(r_num);
		boolean b2=us.addRoomOrder(o);
		if (b1&&b2) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	/*
	 *查询预订弹窗所需参数 
	*/
	private void queryRoomByType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		List<String> r_idlist=us.queryRoomByType(type);
		Map<String, Object> map=new HashMap<>();
		Gson json = new Gson();
		int price=us.queryRoomByTypeToPrice(type);
		String r_price=String.valueOf(price);
		Date date = new Date();
		SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd");
		String indate=aDate.format(date);
		map.put("type", type);
		map.put("indate", indate);
		map.put("r_idlist", r_idlist);
		map.put("price", r_price);
		response.getWriter().println(json.toJson(map));
	}
	/*
	 *管理员修改房间价格 
	*/
	private void updRoomByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String r_type = request.getParameter("r_type");
		int r_price = Integer.valueOf(request.getParameter("r_price"));
		int n = us.updRoomByType(r_type,r_price);
		response.getWriter().print(n);
	}
	/*
	 * 修改用户信息
	*/
	private void updUserById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int u_id = Integer.valueOf(request.getParameter("u_id"));
		String u_name = request.getParameter("u_name");
		String u_pass = request.getParameter("u_pass");
		String u_phone = request.getParameter("u_phone");
		User u=new User();
		u.setU_id(u_id);
		u.setU_name(u_name);
		u.setU_pass(u_pass);
		u.setU_phone(u_phone);
		Map<String, String> map=new HashMap<>();
		Gson json = new Gson();
		HttpSession session = request.getSession();
		User user = us.queryOneUser(u_name, u_pass);
		if (user!=null) {
			map.put("lll", "false");
		}else {
			boolean b = us.updUserById(u);
			if (b) {
				session.setAttribute("user", u);
				map.put("lll", "true");
			}else {
				map.put("lll", "fail");
			}
		}
		response.getWriter().println(json.toJson(map));
	}
	/*
	 * 查询所有房间信息
	*/
	private void listAllRoom(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String search = request.getParameter("search");
		int offset = Integer.valueOf(request.getParameter("offset"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		List<Room> roomList = us.listAllRoom(search, offset, limit);
		int total = us.getRoomTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", roomList);
		Gson gson = new GsonBuilder().create();
		String json = gson.toJson(map);
		response.getWriter().print(json);
	}
	/*
	 * 删除多个所选用户
	*/
	private void removeAllUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String u_ids = request.getParameter("u_ids");
		u_ids = u_ids.substring(1, u_ids.length()-1);
		String[] u_idsArr = u_ids.split(",");
		List<String> list=Arrays.asList(u_idsArr);
		int n = us.removeAllUser(list);
		response.getWriter().print(n);
	}
	/*
	 * 删除单个用户
	*/
	private void removeUserById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int u_id = Integer.valueOf(request.getParameter("u_id"));
		boolean b = us.removeUserById(u_id);
		if(b) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	/*
	 * 查询所有用户信息
	*/
	private void listAllUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String search = request.getParameter("search");
		int offset = Integer.valueOf(request.getParameter("offset"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		List<User> userList = us.listAllSrarch(search, offset, limit);
		int total = us.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", userList);
		Gson gson = new GsonBuilder().create();
		String json = gson.toJson(map);
		response.getWriter().print(json);
	}
	/*
	 * 用户登陆验证
	*/
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");
		Map<String, String> map=new HashMap<>();
		Gson json = new Gson();
		if (name.equals("admin") && pass.equals("admin")) {
			map.put("lll", "right");
		} else {
			HttpSession session = request.getSession();
			if (us.queryOneUser(name, pass) != null) {
				User user = us.queryOneUser(name, pass);
				session.setAttribute("user", user);
				map.put("lll", "true");
			} else {
				map.put("lll", "false");
			}
		}
		response.getWriter().println(json.toJson(map));
	}
	/*
	 * 用户注册验证
	*/
	private void register(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");
		String phone = request.getParameter("phone");
		Map<String, String> map=new HashMap<>();
		Gson json = new Gson();
		HttpSession session = request.getSession();
		User user = us.queryOneUser(name, pass);
		if (user != null) {
			map.put("lll", "false");
		} else {
			User u = new User();
			u.setU_name(name);
			u.setU_pass(pass);
			u.setU_phone(phone);
			us.addUser(u);
			session.setAttribute("u", u);
			map.put("lll", "true");
		}
		response.getWriter().println(json.toJson(map));
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
